﻿// This file is part of GEOTRON.

// GEOTRON is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The GEOTRON library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with GEOTRON.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

using System.IO;

namespace Geotron
{
    public static partial class GeoResolve
    {
        public static LocationResult TryResolvePostcode(string Postcode, Country Region)
        {
            if (Region.GnsFile == false) return new LocationResult(Result.CannotResolve, 0);

            //Postcode = Postcode.Replace(" ", "");

            string Resolvable = Postcode.Substring(0, 2); string Line;

            if (Region.ShortName.ToUpper() == "UK")
            {
                // Open the zip file containing the Code-Point database
                Trace.Write("Opening Code-Point database(Ordnance Survey © Crown copyright 2010)...");
                ICSharpCode.SharpZipLib.Zip.ZipFile PostcodeData;
                try { PostcodeData = new ICSharpCode.SharpZipLib.Zip.ZipFile(new FileStream("Data\\Postcode\\UK\\codepo_gb.zip",FileMode.Open)); }
                catch { Trace.WriteLine("[Failed]"); return new LocationResult(Result.CannotResolve, 0); }
                Trace.WriteLine("[Done]");

                // Open the file containing the region data we need
                Trace.Write("Opening database file for 'Code-Point Open/data/CSV/"+Resolvable.ToLower()+".csv'...");
                long PostCodeDataEntry = PostcodeData.FindEntry("Code-Point Open/data/CSV/"+Resolvable.ToLower()+".csv",true);
                if (PostCodeDataEntry == -1)
                {
                    Trace.WriteLine("[Failed]");
                    return new LocationResult(Result.CannotResolve, 0);
                }

                StreamReader PostCodeFileReader;
                try
                {
                    PostCodeFileReader = new StreamReader(PostcodeData.GetInputStream(PostCodeDataEntry));
                    Trace.WriteLine("[Done]");
                }
                catch
                {
                    Trace.WriteLine("[Failed]");
                    return new LocationResult(Result.CannotResolve, 0);
                }

                Trace.WriteLine("Starting to read from the file...");
                while ((Line = PostCodeFileReader.ReadLine()) != null)
                {
                    Trace.WriteLine("'" + Postcode + "' => '" + Line + "'");
                    if (Line.Contains(Postcode))
                    {
                        Trace.WriteLine("Postcode located!");

                        string[] LineArray = Line.Split(',');

                        UKGridReference Reference = new UKGridReference();

                        Reference.Eastings = Convert.ToInt32(LineArray[10]);
                        Reference.Northings = Convert.ToInt32(LineArray[11]);

                        return new LocationResult(Result.Resolved, 100, Reference.Coordinates, true) ;
                    }
                }
            }

            return new LocationResult(Result.CannotResolve, 0);

        }
    }
}
